package g11.dao;

import g11.model.Appointment;
import g11.model.Visitation;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class VisitationDAOImpl implements VisitationDAO {

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session.isOpen())
			session.close();
		// HibernateUtil.shutdown();
	}

	public VisitationDAOImpl() {

	}

	public void createVisitationFromAppointment(Appointment appointment)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Visitation visitation = new Visitation();
		visitation.setVisitationId(appointment.getAppointmentId());
		visitation.setCurrentVersion(1); // initiate visitation's version as "1"
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.save(visitation);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public void deleteVisitation(Visitation visitation) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.delete(visitation);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public void updateCurrentVersion(Visitation visitation) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(visitation);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public Visitation getCurrentVersionOfVisitation(int visitationId)
			throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Visitation visitation = null;
		try {
			String hql = "from Visitation v where v.visitationId=?";
			Query q = session.createQuery(hql);
			q.setInteger(0, visitationId);
			if (q.list().size() > 0) {
				visitation = (Visitation) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return visitation;
	}

	// for test only
	public void createVisitationById(int visitationId) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Visitation visitation = new Visitation();
		visitation.setVisitationId(visitationId);
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.save(visitation);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

}
